<?php

namespace App\Api\Controllers;

use App\Api\Repositories\Sys\SysSettingRepositories;
use Illuminate\Support\Facades\DB;
use zgldh\QiniuStorage\QiniuStorage;

use App\Api\Service\UserService;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use App\Api\Service\Trigonal\SmsService;
use App\Api\Service\DataService;
use App\Api\Service\SysService;


class BaseController extends Controller
{
    public $nei = [];
    public $wai = [];
    public $title_photo = [];

    public function __construct(Request $request)
    {
        // 获取当前登录的会员信息
        $user_service = new UserService();
        $this->uid = $user_service->use_token_get_uid($request->input('token'));
        // 获取部分系统设置
        $this->setting['identity_field'] = config('admin.users.user_identity')[0];
        $this->title_photo = [
            '全身照', '生活照', '工作照',
            '学习照', '旅行照', '其他照'];
        $this->nei = [
            '全身照' => 'general_photo', '生活照' => 'life_photo', '工作照' => 'work_photo',
            '学习照' => 'study_photo', '旅行照' => 'travel_photo', '其他照' => 'other_photo',
            '性别' => 'sex', '生日' => 'birthday', '颜值标签' => 'face_score',
            '星座' => 'constellation', '身高' => 'height', '微信' => 'weixin',
            'QQ' => 'qq', '邮箱' => 'email', '城市' => 'city',
            '家乡' => 'hometown', '学历' => 'education', '学院' => 'college',
            '学院标签' => 'college_tab', '年收入' => 'income', '婚姻情况' => 'marriage',
            '车房情况' => 'car_and_house', '车房情况详情' => 'car_and_house_detail', '家庭情况' => 'family',
            '户口' => 'account', '行业' => 'industry', '兴趣爱好' => 'hobbies',
            '性格特点' => 'character', '情感经历' => 'emotional_experience', '情感观' => 'emotional_view',
            '择偶要求' => 'mate_selection', '其他' => 'other', '关键词' => 'keyword',
            '头像' => 'avatar', '昵称' => 'nickname'
        ];
        $this->wai = [
            '全身照' => 'general_photo', '生活照' => 'life_photo', '工作照' => 'work_photo',
            '学习照' => 'study_photo', '旅行照' => 'travel_photo', '其他照' => 'other_photo',
            '网址' => 'url', '来源' => 'source', '性别' => 'sex', '生日' => 'birthday',
            '星座' => 'constellation', '身高' => 'height', '所在地' => 'city',
            '年收入' => 'income', '毕业院校' => 'school', '头像' => 'avatar', '昵称' => 'nickname',
            '标签' => 'tags', '家庭' => 'family'
        ];
    }

    public function index_li()
    {
        $nei = DB::table('sys_setting')->where('parent_id', 45)->get()->toArray();
        $wai = DB::table('sys_setting')->where('parent_id', 46)->get()->toArray();

        return success('列表', ['nei' => $this->nei_wai($nei, 1), 'wai' => $this->nei_wai($wai, 2)]);
    }

    public function nei_wai($array, $type = 1)
    {
        $li = $type == 1 ? $this->nei : $this->wai;
        $data = [];
        $num = 0;
        foreach ($array as $value) {
            if (in_array($value->title, $this->title_photo)) {
                if ($value->value != "") {
                    $num = $num + 1;
                }
            }
            $data[$li[$value->title]] = $value->value;
        }
        $data['photo_number'] = $num;
        return $data;
    }

    /**
     * 发送短信验证码
     *
     * @param string $type 场景类型，register(注册), other(其他, 登录、忘记密码、修改密码等)
     * @param int $phone
     * @return void
     */
    public function send_sms(\App\Api\Requests\SendSmsRequest $request)
    {
        $phone = $request->input('phone');
        $sms_service = new SmsService();
        $sms_service->send_sms($phone);
        return success('发送成功');
    }

    /**
     * 获取选项值
     *
     * @param \App\Api\Requests\GetOptionsRequest $request
     * @return void
     */
    public function options(\App\Api\Requests\GetOptionsRequest $request)
    {
        $type = $request->input('type', 'all');
        $service = new DataService;
        return success('选项数据', $service->get_options($type));
    }

    /**
     * 获取指定文章
     *
     * @param Request $request
     * @return void
     */
    public function article(Request $request)
    {
        $id = $request->input('id', 0);
        $service = new DataService;
        return success("文章", $service->get_article($id));
    }

    /**
     * 获取客服信息
     *
     * @param Request $request
     * @return void
     */
    public function service(Request $request)
    {
        $service = new SysService();
        $data = $service->get_ad(11);
        return success("客服", [
            'qq' => $data['客服QQ号'],
            'qrcode' => $data['客服微信二维码'],
            'nickname' => $data['客服昵称'],
            'weixin' => $data['客服微信号']
        ]);
    }

    /**
     * 获取首页数据
     *
     * @param Request $request
     * @return void
     */
    public function index_data(Request $request)
    {
        $service = new SysService();
        return success("首页数据", $service->get_data_count());
    }

    public function Upload(Request $request)
    {
        //检查上传文件中是否包含文件
        if ($request->hasFile('file')) {
            //获取上传文件内容
            $file = $request->file('file');
            $disk = QiniuStorage::disk('qiniu');
            $fileName = md5($file->getClientOriginalName() . time() . rand()) . '.' . $file->getClientOriginalExtension();
            $bool = $disk->put('image/' . $fileName, file_get_contents($file->getRealPath()));
            if ($bool) {
                $path = $disk->downloadUrl('image/' . $fileName);
                return success('上传成功', $path);
            }
            return error('上传失败');
        }
        return error('上传文件不存在');;
    }
}
